package com.secondshop.models;

import java.util.ArrayList;
import java.util.List;

public class Cart {
	private List<Item> items=new ArrayList<Item>();
	private float totalMoney=0;
	private User user;
	private int size;
	public List<Item> getItems() {
		return items;
	}
	public void setItems(List<Item> items) {
		this.items = items;
	}
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}
	public float getTotalMoney() {
		this.totalMoney=0;
		for(Item item:this.items) {
			totalMoney+=item.getSumMoney();
		}
		
		return totalMoney;
	}
	
	public int getSize() {
		return this.items.size();
	}
	public void addCart(Good g) {
		boolean flag=false;
		for(Item item:this.items) {
			
			
			
			if(item.getGood().getId().equals(g.getId())) {
				//说明商品原先存在
				item.setNumber( item.getNumber()+1); 
				flag=true;
				break;
				
			}
		}
			
			if(!flag) {//商品原先不存在
				
			Item item=new Item();
			item.setId(this.items.size()+1);
			item.setGood(g);
			this.items.add(item);
				
			}
		
		}
	//移除
	public void removeItem(Good g) {
		for(Item item:this.items) {
			if(g.getId().equals(item.getGood().getId())) {
				this.items.remove(item);
				break;
				
			}
			
		}
	}

	
		
	
	
}
